package com.engine.cn.sigletom;

public class UnsafeLazySingleton1 {
    //这里主要是防止指令重排序带来的对象半初始化问题
    private static volatile UnsafeLazySingleton1 instance;

    private UnsafeLazySingleton1() {
        System.out.println("UnsafeLazySingleton1 instance");
        if (instance != null) {
            throw new IllegalStateException("Already instanced");
        }
    }

    public static UnsafeLazySingleton1 getInstance() {
        if (instance == null) {
            synchronized (UnsafeLazySingleton1.class) {
                if (instance == null) {
                    instance = new UnsafeLazySingleton1();
                }
            }
        }

        return instance;
    }

    public static void main(String[] args) {
        for (int i = 0; i < 10; i++) {
            new Thread(() -> {
                UnsafeLazySingleton1 instance = getInstance();
                System.out.println("instance==" + instance);
            }).start();
        }
    }
}
